home *** CD-ROM | disk | FTP | other *** search
- Path: mudskipper.cac.psu.edu!user
- From: fcusack@tdx.org (frank.)
- Newsgroups: comp.lang.c
- Subject: Re: How do I round and truncate floats to integers?
- Date: Thu, 15 Feb 1996 18:21:28 -0400
- Organization: Psychic Enemies Network
- Message-ID: <fcusack-1502961821280001@mudskipper.cac.psu.edu>
- References: <4g009b$c2n@news.tuwien.ac.at>
- NNTP-Posting-Host: mudskipper.cac.psu.edu
-
- In article <4g009b$c2n@news.tuwien.ac.at>, sor@rs6.iaee.tuwien.ac.at
- (Evgeni Sorokin) wrote:
-
- > Hello!
- >
- > I am myself ashamed of posting such stupid questions, but FAQ's and
- > books that I've found assume this to be too evident.
- >
- > Given a float (double, to be exact) how do I
- >
- > 1) round it to nearest integer (forget 0.5 problem for the moment) and
- > 2) truncate it to the integer?
-
- typecasting will truncate. To round, add 0.5, then truncate (by
- typecasting). Note that this is not "proper" rounding which changes which
- way the rounding occurs if the decimal is exactly 0.5 depending on whether
- the integer part is even or odd.
- ie,
-
- 5.5 rounds to 6; but 4.5 rounds to 4
- ~Frank
- -- I am Pentium of Borg. Division is futile. You will be approximated. --
- -- If you build it, they will come --> http://www.tdx.org/~fcusack/ --
- -- PGP key fingerprint: 01 C0 C0 B9 CC 78 67 0F 3F 64 80 65 8B 0F F9 EA --
-